<template>
	<view class="home">
		<view class="navBar">
			<image src="../../static/image/logomino.png" style="width: 72rpx;height: 72rpx;" mode=""
				@click="handleLogo"></image>
			<view class="inputBox" @click="handleSearch">
				<u-icon name="search" color="#CCCCCC" size="25" style="margin-left: 164rpx;"></u-icon>
				<view class="">
					搜索关键词
				</view>
			</view>
			<image src="../../static/image/gg.png" style="width: 72rpx;height: 72rpx;" @click="handleGG" mode="">
			</image>
		</view>

		<view class="" style="height: 176rpx;"></view>

		<view class="bannerBox">
			<swiper class="swiper" circular :indicator-dots="indicatorDots" :autoplay="autoplay"
				indicator-active-color="#FF465E" style="width: 100%;height: 280rpx;">
				<swiper-item v-for="(item,index) in bannerList" :key="index" style="text-align: center;">
					<image class="contentImg" :src="item.img_url"
						style="width: 100%;height: 240rpx;z-index: 999; border-radius: 20rpx;" mode="aspectFill">
					</image>
				</swiper-item>
			</swiper>
			<image src="../../static/image/yy.png" style="width: 626rpx;height:26rpx;top: -25rpx;left: 32rpx;" mode="">
			</image>
		</view>

		<view class="contentBox">
			<!-- 分类 -->
			<view class="classfiyBox">
				<view class="classfiyItem" v-for="(item,index) in categoryList" :key="index"
					@click="handleCategory(index)">
					<image :src="item.image" style="width: 88rpx;height: 88rpx;border-radius: 88rpx;" mode="">
					</image>
					<view class="">
						{{item.title}}
					</view>
				</view>
			</view>

			<!-- 公告 -->
			<view class="noticeBox" @click="handleGG">
				<u-notice-bar :text="text1" direction="column" @click="handleunoticebar"></u-notice-bar>
			</view>


			<view class="tabBox">
				<image src="../../static/image/tabs.png" style="width: 100%;height: 122rpx;" mode=""></image>
				<view class="innerBox">
					<view class="inneritem" style="margin-left: 196rpx;" @click="handleTabs(0)">
						<view class="top" :class="currIndex==0?'toped' :''">
							热销商品
						</view>
						<view class="bottom" :class="currIndex==0?'bottomed' :''">
							大家都在买
						</view>
					</view>

					<view class="inneritem" style="margin-left: 100rpx;" @click="handleTabs(1)">
						<view class="top" :class="currIndex==1?'toped' :''">
							特价促销
						</view>
						<view class="bottom" :class="currIndex==1?'bottomed' :''">
							今日特价
						</view>
					</view>
				</view>
			</view>

			<view class="dataList">
				<view class="dataItem" v-for="(item,index) in productslist" :key="index" @click="handleDetail(item)">
					<image :src="item.image" style="width: 336rpx;height: 336rpx;border-radius: 20rpx;"
						mode="aspectFill"></image>
					<view class="" style="padding: 0 10rpx; box-sizing: border-box;">
						<view class="name">
							{{item.name}}
						</view>
						<view class="desc">
							{{item.describe}}
						</view>
						<view class="priceBox">
							<view class="price">
								₱{{item.sku_list[0].price}}
							</view>
							<view class="rightprice">
								<!-- {{item.unit ? item.unit :"份"}} -->
								/{{item.sku_list[0].sku_text}}
								<text v-if="item.market_price">₱{{item.market_price}}</text>
							</view>
						</view>

						<view class="bottomBox">
							<view class="ys">
								已售{{item.sold_count}}
							</view>
							<image src="../../static/image/tocar.png" style="width: 48rpx;height: 48rpx;" mode=""
								@click="addcart(item)">
							</image>
						</view>
					</view>

					<view v-if="item.weight==0" class="sc_box" @click="handleSxBox">
						<image class="sxicon" src="../../static/image/sx.png" mode=""></image>
					</view>

				</view>
			</view>

		</view>

		<view class="" style="height: 100rpx;"></view>

		<vk-data-goods-sku-popup ref="skuPopup" v-model="skuKey" border-radius="20" :localdata="goodsInfo"
			:mode="skuMode" @add-cart="handleAddcart" buyNowBackgroundColor="#FF3D3D"></vk-data-goods-sku-popup>
	</view>
</template>

<script>
	import {
		bannerList,
		categoryList,
		productslist,
		addshopcar,
		activity,
		cardNum
	} from "@/api/api.js"
	export default {
		data() {
			return {
				text1: [],
				currIndex: 0,
				bannerList: [],
				indicatorDots: false,
				autoplay: true,
				categoryList: [],
				productslist: [],
				is_hot: 1,
				is_new: 0,
				page: 1,
				page_size: 10,
				skuKey: false, // 是否打开SKU弹窗
				skuMode: 1, // SKU弹窗模式
				goodsInfo: {}, // 后端返回的商品信息
			}
		},
		onLoad() {
			this.handlebannerList()
			this.handlecategoryList()
			this.handleproductslist()
			this.handleactivity()
			this.handleCarnum()
		},
		onReachBottom() {
			this.page++;
			this.handleproductslist();
		},
		methods: {
			handleLogo() {
				// console.log("22222222")
			},
			async handlebannerList() {
				let res = await bannerList()
				this.bannerList = res.data
			},
			async handlecategoryList() {
				let res = await categoryList()
				this.categoryList = res.data
			},
			async handleproductslist() {
				let res = await productslist({
					is_hot: this.is_hot,
					is_new: this.is_new,
					page: this.page,
					page_size: this.page_size,
					is_all:1,

				})
				res.data.product.data.forEach((item, index) => {
					this.productslist.push(item)
				})
			},
			handleTabs(index) {
				this.currIndex = index
				this.page = 1
				if (index == 0) {
					this.is_hot = 1
					this.is_new = 0
				} else {
					this.is_hot = 0
					this.is_new = 1
				}
				this.productslist = []
				this.handleproductslist()
			},
			handleSearch() {
				uni.navigateTo({
					url: "./search"
				})
			},
			addcart(data) {
				event.stopPropagation()
				// console.log("=========", data)
				let that = this
				that.goodsInfo = {
					"_id": data.sku_list[0].id,
					"name": data.sku_list[0].sku_name,
					"goods_thumb": data.image,
					"sku_list": data.sku_list,
					"spec_list": data.spec_list
				}
				that.skuKey = true;
			},
			async handleAddcart(selectShop) {
				// console.log("selectShop", selectShop)
				let data = {
					products_id: selectShop.products_id,
					sku_num_id: selectShop.id,
					num: selectShop.buy_num
				}
				// console.log("data", data)
				let res = await addshopcar(data)
				uni.showToast({
					title: res.message,
					icon: "none", 
					duration: 2000
				});
				if (res.code == 1000) {
					this.skuKey = false;
					this.handleCarnum()
				}
			},
			handleDetail(item) {
				uni.navigateTo({
					url: `./detail?id=${item.id}`
				})
			},

			handleGG() {
				uni.navigateTo({
					url: "../mine/announcementList"
				})
			},

			handleCategory(index) {
				uni.setStorageSync('classifyIndex', index + 1)
				uni.switchTab({
					url: "/pages/classify/index"
				})
			},

			async handleactivity() {
				let res = await activity()
				// console.log("333",res)
				res.data.forEach((item, index) => {
					this.text1.push(item.title)
				})

			},

			handleunoticebar(e) {
				// console.log("3333", e)
			},

			handleSxBox() {
				uni.showToast({
					title: '该商品已售罄！',
					icon: "none",
					duration: 2000
				});
				event.stopPropagation()
			},
			async handleCarnum() {
				let res = await cardNum()
				if (res.data > 0) {
					uni.setTabBarBadge({
						index: 2,
						text: res.data.toString()
					})
				}
			},

		},


	}
</script>

<style scoped lang="scss">
	.home {
		width: 100%;
		min-height: 100%;
		background-color: #ffffff;

		.navBar {
			width: 100%;
			height: 176rpx;
			background-color: #439B05;
			padding: 0 30rpx;
			padding-top: 82rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: space-between;
			position: fixed;
			top: 0;
			z-index: 99999;

			.inputBox {
				width: 506rpx;
				height: 80rpx;
				background-color: #ffffff;
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				color: #CCCCCC;
				line-height: 80rpx;
				display: flex;
				align-content: center;
				border-radius: 200rpx;
			}
		}

		.bannerBox {
			width: 100%;
			height: 432rpx;
			background-color: #439B05;
			padding: 20rpx 30rpx;
			box-sizing: border-box;
			position: relative;
		}

		.contentBox {
			width: 100%;
			transform: translate(0rpx, -142rpx);
			border-radius: 40rpx 40rpx 0rpx 0rpx;
			background-color: #ffffff;

			.classfiyBox {
				width: 100%;
				// height: 336rpx;
				padding: 30rpx 54rpx;
				box-sizing: border-box;
				display: flex;
				flex-wrap: wrap;


				.classfiyItem {
					// margin-right: 45rpx;
					font-size: 24rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #666666;
					line-height: 28rpx;
					text-align: center;
					margin-bottom: 20rpx;
					width: 20%;

				}

				// .classfiyItem:nth-child(5n) {
				// 	margin-right: 0;
				// }
			}

			.noticeBox {
				width: 100%;
				box-sizing: border-box;
				padding: 0 30rpx;
				box-sizing: border-box;
			}

			.tabBox {
				width: 100%;
				margin-top: 16rpx;
				position: relative;

				.innerBox {
					position: absolute;
					top: 0;
					display: flex;
					align-items: center;
					padding-top: 20rpx;
					box-sizing: border-box;

					.inneritem {
						text-align: center;

						.top {
							font-size: 32rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							color: #333333;
							line-height: 38rpx;
						}

						.bottom {
							font-size: 24rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							color: #999999;
							line-height: 28rpx;
							margin-top: 5rpx;
						}

						.toped {
							color: #439B05;
						}

						.bottomed {
							color: #ffffff;
							width: 128rpx;
							height: 34rpx;
							border-radius: 100rpx;
							font-size: 24rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							background-color: #439B05;
							line-height: 34rpx;
							text-align: center;
						}
					}
				}
			}

			.dataList {
				width: 100%;
				padding: 0 30rpx;
				box-sizing: border-box;
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;

				.dataItem {
					width: 336rpx;
					height: 580rpx;
					background: #FFFFFF;
					box-shadow: 0rpx 4rpx 12rpx 0rpx rgba(0, 0, 0, 0.08);
					border-radius: 20rpx 20rpx 20rpx 20rpx;
					margin-bottom: 20rpx;
					position: relative;

					.sc_box {
						position: absolute;
						top: 0;
						left: 0;
						// z-index: 999999;
						width: 336rpx;
						height: 580rpx;
						border-radius: 20rpx 20rpx 20rpx 20rpx;
						background-color: black;
						opacity: 0.5;
						display: flex;
						flex-direction: column;
						align-items: center;

						.sxicon {
							width: 300rpx;
							height: 300rpx;
						}
					}

					.name {
						width: 316rpx;
						// height: 80rpx;
						font-size: 28rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						color: #333333;
						margin-bottom: 10rpx;
						line-height: 40rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;

					}

					.desc {
						font-size: 20rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						color: #439B05;
					}

					.priceBox {
						display: flex;
						margin-top: 10rpx;

						.price {
							font-size: 28rpx;
							font-family: DIN, DIN;
							font-weight: 500;
							color: #FF3D3D;
							line-height: 33rpx;
						}

						.rightprice {
							font-size: 24rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							color: #BBBBBB;
							line-height: 28rpx;
						}
					}

					.bottomBox {
						display: flex;
						width: 100%;
						justify-content: space-between;
						font-size: 20rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						color: #666666;
						line-height: 23rpx;
						align-items: center;
						margin-top: 16rpx;
					}
				}
			}
		}
	}
</style>